//
//  NSString+LSCategory.m
//  比较时间字符串大小
//
//  Created by chenleping on 2018/12/12.
//  Copyright © 2018年 DSY. All rights reserved.
//

#import "NSString+LSCategory.h"

@implementation NSString (LSCategory)

// 字符串转NSDate
- (NSDate *)ls_convertToDateWithFormat:(NSString *)format{
    NSDateFormatter * formatter = [NSDateFormatter new];
    [formatter setDateFormat:format];
    NSDate * date = [formatter dateFromString:self];
    return date;
}
// 是否包含汉字
- (BOOL)ls_includeChinese{
    for(int i=0; i< [self length];i++)
    {
        int a =[self characterAtIndex:i];
        if( a >0x4e00&& a <0x9fff){
            return YES;
        }
    }
    return NO;
}
//  判断是否是电话号码
- (BOOL)ls_isValidPhoneNum{
    NSString *phoneRegex =  @"^1(3[0-9]|4[57]|5[0-35-9]|8[025-9])\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:self];
}

//  判断是否是邮箱
- (BOOL)ls_isValidEmail{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}
- (int)ls_compareWithAnotherDateStr:(NSString *)anotherDateStr{
    
    NSComparisonResult result = [self compare:anotherDateStr];
    int comparisonResult;
    if (result == NSOrderedAscending) {
        comparisonResult = -1;
    }else if (result == NSOrderedDescending){
        comparisonResult = 1;
    }else {
        comparisonResult = 0;
    }
    return comparisonResult;
}

@end
